package frontend_package;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import backend_package.Login;
import backend_package.Packet;

/**
 * The user can login in by entering a username and password. If the credentials
 * are invalid, a message will be displayed. Users can also exit the program
 * here. Logging out should return a user to this panel.
 * 
 * @author Kyler Nelson
 * 
 */
public class LoginPanel extends Observable implements Observer {

	private ImagePanel loginPanel;
	private JTextField txtUsername;
	private JPasswordField txtPassword;
	private GridBagConstraints gbc = new GridBagConstraints();

	private JButton btnEnter;
	private JButton btnExit;
	private Image myPicture = null;
	private String username = "";
	private String password = "";

	/**
	 * @author Kyler
	 */
	public LoginPanel() {
		this("", "");
	}

	/**
	 * @author Kyler
	 * @author Aidana - edited
	 * @param username
	 * @param password
	 */
	public LoginPanel(String username, String password) {

		URL url = getClass().getResource("/4.jpg");
		Image image = Toolkit.getDefaultToolkit().getImage(url);

		myPicture = image;

		loginPanel = new ImagePanel(myPicture, new GridBagLayout());
		this.txtUsername = new JTextField(10);
		this.txtPassword = new JPasswordField(10);

		btnEnter = new JButton("Enter");

		btnExit = new JButton("Exit");
		customizeButtons();

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.CENTER;
		loginPanel.add(new JLabel("Username:"), gbc);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.CENTER;
		loginPanel.add(txtUsername, gbc);

		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.CENTER;
		loginPanel.add(new JLabel("Password:"), gbc);
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.CENTER;
		loginPanel.add(txtPassword, gbc);

		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.insets = new Insets(10, 0, 0, 0);
		loginPanel.add(btnEnter, gbc);

		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.insets = new Insets(10, 10, 0, 0);
		loginPanel.add(btnExit, gbc);

		addListeners();
	}

	public void paintComponent(Graphics g) {
		g.drawImage(myPicture, 0, 0, null);
	}

	/**
	 * Fun design for buttons
	 * 
	 * @author Aidana
	 */
	private void customizeButtons() {

		btnEnter.setPreferredSize(new Dimension(78, 40));
		btnEnter.setBorder(new RoundedBorder(20));
		
		URL url = getClass().getResource("/login2.jpg");
		Image image = Toolkit.getDefaultToolkit().getImage(url);
		btnEnter.setIcon(new ImageIcon(image));
		btnEnter.setRolloverEnabled(true);

		URL url2 = getClass().getResource("/logging.jpg");
		Image image2 = Toolkit.getDefaultToolkit().getImage(url2);
		btnEnter.setRolloverIcon(new ImageIcon(image2));

		btnExit.setPreferredSize(new Dimension(80, 38));
		btnExit.setBorder(new RoundedBorder(18));
			
		URL url3 = getClass().getResource("/exit2.jpg");
		Image image3 = Toolkit.getDefaultToolkit().getImage(url3);
		btnExit.setIcon(new ImageIcon(image3));
		btnExit.setRolloverEnabled(true);
		
		URL url4 = getClass().getResource("/exiting.jpg");
		Image image4 = Toolkit.getDefaultToolkit().getImage(url4);
		btnExit.setRolloverIcon(new ImageIcon(image4));
	}

	/**
	 * Adds listeners to buttons
	 * 
	 * @author Syrym
	 */
	private void addListeners() {
		btnEnter.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				String username = txtUsername.getText();
				String password = "";
				char[] charArray = txtPassword.getPassword();
				for (int i = 0; i < charArray.length; i++) {
					password = password + charArray[i];
				}

				if (!username.isEmpty() && !password.isEmpty()) {
					Packet packet = new Packet("lp_isvaliduser", new String[] {
							username, password });
					setChanged();
					notifyObservers(packet);
					username = "";
					password = "";
				} else {
					JOptionPane.showMessageDialog(loginPanel,
							"Invalid username and/or password");
				}
			}

		});
		btnExit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				setChanged();
				notifyObservers(new Packet("lp_exitframe"));
			}
		});
	}

	/**
	 * @author Kyler
	 * @return loginPanel
	 */
	public JPanel getPanel() {
		return loginPanel;
	}

	/**
	 * 
	 * @author Kyler
	 * @author Syrym - edited
	 * @return userName
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * 
	 * @author Kyler
	 * @param username
	 */
	public void setUsername(String username) {
		this.txtUsername.setText(username);
	}

	/**
	 * 
	 * @author Kyler
	 * @author Syrym - edited
	 * @return
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * 
	 * @author Kyler
	 * @param password
	 */
	public void setPassword(String password) {
		this.txtPassword.setText(password);
	}

	/**
	 * Updates the packet, notify users
	 * 
	 * @author Kyler
	 */
	@Override
	public void update(Observable o, Object arg) {
		Packet packet = (Packet) arg;
		if (o instanceof Login && packet.getTag().equals("l_notuser")) {
			JOptionPane.showMessageDialog(loginPanel,
					"Invalid username and/or password");
		}
	}

	/**
	 * Class that draws background image to panel
	 * 
	 * @author Aidana
	 * 
	 */
	@SuppressWarnings("serial")
	class ImagePanel extends JPanel {

		private Image img;

		public ImagePanel(Image img, LayoutManager layout) {
			this.img = img;
			
			Dimension size = new Dimension(img.getWidth(null),
					img.getHeight(null));
			setPreferredSize(size);
			setMinimumSize(size);
			setMaximumSize(size);
			setSize(size);
			setLayout(null);
			super.setLayout(layout);
		}

		public void paintComponent(Graphics g) {
			g.drawImage(img, 0, 0, null);
		}

	}

}
